package com.chenfan.ccp.plug.log.util;

import com.chenfan.ccp.handler.BaseHttpResponse;
import com.chenfan.ccp.utils.sign.IpCryptUtil;
import com.chenfan.ccp.utils.sign.SignUtil;
import com.chenfan.ccp.utils.tools.IpTool;
import org.slf4j.MDC;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.UUID;

public class TraceIdUtil {
    public final static String SHORT_TRACE_ID = "traceId";
    public final static String TENANTID = "tenantId";
    public final static String USERID = "userId";
    public final static String EMPTY = "";

    /**
     * 清除跟踪
     */
    public static void clearTrace() {
        MDC.clear();
    }

    public static String getTraceId() {
        return getMDC(SHORT_TRACE_ID);
    }

    public static String getMDC(String key) {
        return MDC.get(key);
    }







    public static void logMDC(HttpServletRequest request, BaseHttpResponse response, String key, String value) {
        if (Objects.isNull(value)){
            return;
        }
        //httpHeaders 封装了所有的请求头
        String oldValue = request.getHeader(key);
        String newValue = Objects.isNull(oldValue) ? value : oldValue;
        MDC.put(key, newValue);
        //设置增强的request到exchange对象中
        if(TraceIdUtil.SHORT_TRACE_ID.equals(key) && Objects.isNull(oldValue)){
            response.setHeader(key,newValue);
        }
    }

    /**
     * 生成32位不重复uuid
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 跟踪Rpc
     */
    @Deprecated
    public static void traceRpc(String traceId) {
        if (traceId == null || traceId.isEmpty()) {
            return;
        }
        MDC.put(SHORT_TRACE_ID, traceId == null ? EMPTY : traceId);
    }

    /**
     * 通过踪转换request获取跟踪Id
     *
     * @param request 原来的请求
     */
    @Deprecated
    public static String getTraceIdByConvertRequest(HttpServletRequest request, boolean spanIdFlag) {

        String shortTraceId = EMPTY;
        if (request == null) {
            return shortTraceId;
        }

        shortTraceId = request.getHeader(SHORT_TRACE_ID);
        if (!(shortTraceId == null || shortTraceId.isEmpty())) {
            return shortTraceId;
        }


        String originalTraceId = request.getHeader("user-agent");

        //这里进行traceID和spanId的结合
        String ip = IpTool.getRealIp(request);
        shortTraceId = SignUtil.sign((originalTraceId == null ? EMPTY : originalTraceId)+System.currentTimeMillis(), SignUtil.BIT._62) + IpCryptUtil.encode(ip);
        return shortTraceId;
    }





    /**
     * 跟踪request
     */
    @Deprecated
    public static String traceRequest(HttpServletRequest request, BaseHttpResponse response) {
        if (request == null) {
            return null;
        }
        String traceId = request.getHeader(SHORT_TRACE_ID);
        traceId = Objects.isNull(traceId) ? getUUID() : traceId;
        MDC.put(SHORT_TRACE_ID, traceId);
        if(Objects.isNull(response.getHeader(SHORT_TRACE_ID))){
            response.setHeader(SHORT_TRACE_ID, traceId);
        }
        return traceId;
    }



}
